package fms.models;
 
import java.util.List;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import fms.classes.userOrganization;
 
 
public class userOrganizationDAO {
 
    private SqlSessionFactory sqlSessionFactory;
 
    public userOrganizationDAO(){
    	
    	try {
    		sqlSessionFactory = MyBatisSqlConnection.getSqlSessionFactory();
    	} catch(Exception e) {
    		System.out.println("failed to get sqlSessionFactory");
    	}
    }
 
    /**Returns the list of all userOrganization instances from the database*/
    @SuppressWarnings("unchecked")
    public List<userOrganization> selectAll(){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            List<userOrganization> list = session.selectList("userOrganization.getAll");
            return list;
        } finally {
            session.close();
        }
    }
 
    /**Returns a userOrganization instance from the database*/
    public userOrganization selectById(int account_id){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            userOrganization userOrg = (userOrganization) session.selectOne("userOrganization.getById",account_id);
            return userOrg;
        } finally {
            session.close();
        }
    }
 
    /**Updates an instance of userOrganization in the database*/
    public void update(userOrganization userOrg){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.update("userOrganization.update", userOrg);
            session.commit();
        } finally {
            session.close();
        }
    }
 
    /** Insert an instance of userOrganization into the database*/
    public void insert(userOrganization userOrg){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.insert("userOrganization.insert", userOrg);
            session.commit();
        } finally {
            session.close();
        }
    }
 
    /** Delete an instance of userOrganization from the database*/
    public void delete(int account_id){
 
        SqlSession session = sqlSessionFactory.openSession();
 
        try {
            session.delete("userOrganization.deleteById", account_id);
            session.commit();
        } finally {
            session.close();
        }
    }
}